<?php
	require("includes/basic.php");
	if(isset($_POST['id']))
	{
		$phaseid=$_POST['id'];
		$projectid=$_POST['q'];
		$pm = $_POST['pm'];
		$checkph = mysql_query("select * from project_phase where project_id=$projectid and phase_id=$phaseid");
		if(mysql_num_rows($checkph) >0)
		{
			?>
			<script type="text/javascript">
			alert("This phase has already been added to the project");
			</script>
			<?php
		
		}
		else
		{
		$addphase=mysql_query("insert into project_phase (`project_id`,`phase_id`) values ('$projectid','$phaseid')");
			
		}
	}
	
	if(isset($_POST['r']) && isset($_POST['ap']) && isset($_POST['q']))	
	{		
		$assignto=$_POST['r'];
		$projid=$_POST['q'];		
		$sid = split(',',$_POST['ap']);
		$pm = $_POST['pm'];
		foreach($sid as $p => $sid)
		{			
			$assignphase=mysql_query("insert into task_assigned_to(`task_assigned_to`,`task_assigned_on`,`task_assigned_by`,`project_id`,`phase_id`,`role_id`) values('$assignto',sysdate(),'$userId','$projid','$sid','2')");
		}
		$projectid=$projid;
	}
	if(isset($_POST['msid']) && isset($_POST['mid']) && isset($_POST['mq']))	
	{
		$mid = $_POST['mid'];
		$projectid = $_POST['mq'];
		$sid = split(',',$_POST['msid']);
		$pm = $_POST['pm'];
		foreach($sid as $ssid => $s)
		{
			$checkright = mysql_query("select * from task_assigned_to where project_id=$projectid and phase_id=$s and role_id=2 and task_assigned_to=$userId");
			
			if(mysql_num_rows($checkright) > 0 || $userType=='O' || $userId == $pm)
			{
				$checkm = mysql_query("select * from phase_module where project_id=$projectid and phase_id=$s and module_id=$mid");
				if(mysql_num_rows($checkm)>0)
				{
					?>
					<script type="text/javascript">
					alert("This module already added to this phase");
					</script>
					<?php
				}
				else
				{
				$addmod=mysql_query("insert into phase_module (`project_id`,`phase_id`,`module_id`) values ('$projectid','$s','$mid')");
				}
			}
			else
			{
				?>
					<script type="text/javascript">
					alert("You can add modules to only those phases for which you are phase manager");
					</script>
					<?php
			}
			
		}
	}
	
	if(isset($_POST['r']) && isset($_POST['mid']) && isset($_POST['q']))	
	{	
		$assignto=$_POST['r'];
		$projectid=$_POST['q'];	
		
		$pmid = split(',',$_POST['mid']);	
		$pm = 	$_POST['pm'];	
		foreach($pmid as $m => $smid)
		{	
			$se = 	explode('|', $smid, 2);	
			
			$checkrightam = mysql_query("select * from task_assigned_to where project_id=$projectid and phase_id=$se[0] and role_id=2 and task_assigned_to=$userId");
			if(mysql_num_rows($checkrightam) > 0 || $userType=='O' || $userId == $pm)
			{
			$assignmod=mysql_query("insert into task_assigned_to (`task_assigned_to`,`task_assigned_on`,`task_assigned_by`,`project_id`,`phase_id`,`module_id`,`role_id`) values ('$assignto',sysdate(),'$userId','$projectid','$se[0]','$se[1]','3')"); 
			}
			else
			{
				?>
					<script type="text/javascript">
					alert("You can assign modules of only those phases for which you are phase manager");
					</script>
					<?php
			}
		}
		
	}
	?>
	
<?php
	echo "<table id='cb' border='0' width='100%'><tr><td>";
	$getphases=mysql_query("select * from project_phase pp, phase_master pm where pp.phase_id=pm.phase_id and pp.project_id=$projectid");
	while($getphasesr=mysql_fetch_array($getphases))
	{
		$phaseId=$getphasesr['phase_id'];
		echo "<table border='0' width='100%' style='float:left; margin-top:5px; border:1px solid;'>";
		echo "<tr><td><input type='checkbox' value='$phaseId' name='checkp' id='checkp'/>&nbsp;<b>".$getphasesr['phase']."</b></td>";
		echo "<td width='10px' >";
		if($userId == $pm || $userType=='O')
				{
		echo "<a id='rmvphase' href='delphase.php?pid=$projectid&sid=$phaseId' name='rmvphase' value='' ><img Title='Remove Phase' alt='Remove Phase'src='images/multi_delete.png'></a>";
		}
		echo "</td></tr>";
		echo "<tr><td><b>Description : </b>".$getphasesr['phase_desc']."</td></tr>";
		$phasemanager=mysql_query("select * from task_assigned_to t, user_master u where t.project_id=$projectid and t.phase_id=$phaseId and t.role_id='2' and t.task_assigned_to=u.user_id");
		echo "<tr><td><b>Phase Manager : </b>";
		while($phaseman=mysql_fetch_array($phasemanager))
		{
			echo $phaseman['first_name']." ".$phaseman['last_name']."<br/>";
		}
			echo "</td></tr>";
			$phaseId=$getphasesr['phase_id'];
		echo "<tr><td><b><u>Modules</u> : </b><br/>";
		$getmodules=mysql_query("select * from phase_module pm, module_master mm where pm.project_id=".$projectid." and pm.phase_id=$phaseId and pm.module_id=mm.module_id");
		if(mysql_num_rows($getmodules)>0)
		{
			while($getmodulesr=mysql_fetch_array($getmodules))
			{
				$moduleid=$getmodulesr['module_id'];
				
				echo "<table width='280px' style='border:1px solid;margin-left:5px; margin-top:5px;' align='left'>
				<tr><td><input type='checkbox' name='checkm' value='$phaseId|$moduleid' id='checkm'/>&nbsp;";
				echo "<a alt='View Tasks' title='View Tasks' href='view-task.php?pid=$projectid&sid=$phaseId&mid=$moduleid'><img src='images/open-project.png'></a>";
				echo $getmodulesr['module']."</td><td>";
				if($userId == $pm || $userType=='O')
				{
				echo "<a id='rmvphase' href='delmodule.php?pid=$projectid&sid=$phaseId&mid=$moduleid' name='rmvmodule' value='' ><img Title='Remove Module' alt='Remove Module'src='images/multi_delete.png'></a>";
				}
				echo "</td></tr>";
				echo "<tr><td><b><u>Module Description</u> : </b>".$getmodulesr['module_desc']."</td><td>&nbsp;</td></tr>";
				$modulemanager=mysql_query("select * from task_assigned_to t, user_master u where u.user_id=t.task_assigned_to and t.project_id=$projectid and t.phase_id=$phaseId and t.module_id=$moduleid and t.role_id='3'");
				echo "<tr><td><b><u>Module Manager</u> : </b>";
				while($modman=mysql_fetch_array($modulemanager))
				{
					echo $modman['first_name']." ".$modman['last_name']."<br/>";
				}
				echo "</td><td>&nbsp;</td></tr></table>";
			}
		}
		else{
		echo "No Modules added";
		}
		echo "</td></tr>";
		echo "</table>";
		
	}
	echo "</td></tr>";
	echo "<tr><td><br/></td></tr>";
	echo "</table>"
	
?>